home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pgp20src.zip / PASSWD.C < prev    next >
C/C++ Source or Header  |  1992-08-20  |  2KB  |  78 lines

  1. /*    passwd.c - Password reading/hashing routines
  2.     (c) 1989 Philip Zimmermann.  All rights reserved.
  3.     Implemented in Microsoft C.
  4.     Routines for getting a pass phrase from the user's console.
  5. */
  6.  
  7. #include    <stdio.h>    /* for fprintf() */
  8. #include    <ctype.h>    /* for isdigit(), toupper(), etc. */
  9. #include    <string.h>    /* for strlen() */
  10.  
  11. #include    "random.h"    /* for getstring() */
  12. #include    "md5.h"
  13. #include    "language.h"
  14. #include    "pgp.h"
  15.  
  16. #define MAXKEYLEN 254    /* max byte length of pass phrase */
  17.  
  18. boolean showpass = FALSE;
  19.  
  20. /*
  21. **    hashpass - Hash pass phrase down to 128 bits (16 bytes).
  22. **  keylen must be less than 1024.
  23. **    Use the MD5 algorithm.
  24. */
  25. void hashpass (char *keystring, int keylen, byte *hash)
  26. {
  27.     MD5_CTX    mdContext;
  28.     int        i;
  29.  
  30.     /* Calculate the hash */
  31.     MD5Init(&mdContext);
  32.     MD5Update(&mdContext, (unsigned char *) keystring, keylen);
  33.     MD5Final(&mdContext);
  34.     /* Copy it to return variable */
  35.     memcpy(hash, mdContext.digest, 16);
  36. }    /* hashpass */
  37.  
  38.  
  39. /*
  40. **    getideakey - get pass phrase from user, hashes it to an IDEA key.
  41.     Parameters:
  42.         returns char *keystring as the pass phrase itself
  43.         return char *hash as the 16-byte hash of the pass phrase
  44.                 using MD5.
  45.         byte noecho:  
  46.             0=ask once, echo. 
  47.             1=ask once, no echo. 
  48.             2=ask twice, no echo.
  49.     Return 0 if no characters are input, else return 1.
  50.     If we return 0, the hashed key will not be useful.
  51. */
  52. int getideakey(char *keystring, char *hash, boolean noecho)
  53. {    char keystr2[MAXKEYLEN+2];
  54.     int len;
  55.  
  56.     if (showpass)
  57.         noecho = 0;
  58.     while (TRUE) {
  59.         fprintf(pgpout,PSTR("\nEnter pass phrase: "));
  60.         getstring(keystring,MAXKEYLEN-1,!noecho);
  61.         if (noecho<2)    /* no need to ask again if user can see it */
  62.             break;
  63.         fprintf(pgpout,PSTR("\nEnter same pass phrase again: "));
  64.         getstring(keystr2,MAXKEYLEN-1,!noecho);
  65.         if (strcmp(keystring,keystr2)==0)
  66.             break;
  67.         fprintf(pgpout,PSTR("\n\007Error: Pass phrases were different.  Try again."));
  68.     }
  69.  
  70.     len = strlen(keystring);
  71.     if (len == 0)
  72.         return 0;
  73.     INTERNAL(keystring);
  74.     hashpass (keystring, strlen(keystring), (byte *) hash);
  75.     return 1;
  76. }    /* getideakey */
  77.  
  78.